package com.qf.zyh.services.Impl;

import com.qf.zyh.dao.AddressDao;
import com.qf.zyh.dao.Impl.AddressDaoImpl;
import com.qf.zyh.entity.Address;
import com.qf.zyh.services.AddressSer;
import com.qf.zyh.util.DBUtil;

import java.util.List;

public class AddressSerImpl implements AddressSer {
    AddressDao ad= new AddressDaoImpl();

    @Override
    public List<Address> seleAddr(int uid) {
        try {
            DBUtil.begin();
            List<Address> list = ad.seleAddr(uid);
            System.out.println(list);
            DBUtil.commit();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        return null;
    }

    @Override
    public boolean addAddr(Address ar) {
        try {
            DBUtil.begin();
            int i = ad.addAddr(ar);
            DBUtil.commit();
            if (i>0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        return false;
    }

    @Override
    public boolean deleAddr(Integer aid) {
        try {
            DBUtil.begin();
            int a = ad.deleAddr(aid);
            DBUtil.commit();
            if (a>0){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        return false;
    }

    @Override
    public boolean updastate(Integer aid,int uid) {
        try {
            DBUtil.begin();
            //设置地址为1
            int i= ad.updastate(aid);
            //将除了该id的都设置为0
            int y = ad.updastatee(aid,uid);
            DBUtil.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        return false;
    }

    @Override
    public boolean updateAddr(Address a) {
        try {
            DBUtil.begin();
            int i = ad.updateaddr(a);
            DBUtil.commit();
            if (i>0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        return false;
    }
}
